
#include <gtk-3.0/gtk/gtk.h>
#include <string.h>
#include "audio_dev_box_widget.h"


typedef struct audio_dev_box_widget_private AudioDevBoxWidgetPrivate;

struct _audio_dev_box_widget 
{
    GtkBox parent;
    AudioDevBoxWidgetPrivate *priv;
    
};

struct _audio_dev_box_widget_class
{
    GtkBoxClass parent_class;
};

struct audio_dev_box_widget_private 
{
    GtkWidget *audio_dev_server_label,*audio_dev_name_label,*audio_dev_desc_label,*audio_dev_available_img,
                *audio_dev_checkbutton;
    
};

G_DEFINE_TYPE_WITH_PRIVATE(AudioDevBoxWidget,audio_dev_box_widget,GTK_TYPE_BOX);

enum {
    PROP_0,
    PROP_SERVER_NAME,
    PROP_DEVICE_NAME,
    PROP_DEVICE_DESC,
    PROP_IS_AVAILABLE,
    PROP_IS_ENABLED,
    N_PROPERTIES
};

static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };

static void audio_dev_box_widget_init(AudioDevBoxWidget *widget) {
    widget->priv = G_TYPE_INSTANCE_GET_PRIVATE(widget, AUDIO_DEV_BOX_WIDGET_TYPE, AudioDevBoxWidgetPrivate);
    gtk_widget_init_template(GTK_WIDGET(widget));    
}


AudioDevBoxWidget *audio_dev_box_widget_new(const char *server_name,const char *device_name, const char *description, gboolean is_available, gboolean is_enabled) {
    // can add more parameters here
    return g_object_new(AUDIO_DEV_BOX_WIDGET_TYPE,"server_name",server_name,"device_name",device_name,
                        "device_desc",description,"device_is_available",is_available,"device_is_enabled",is_enabled,NULL);
}

static void audio_dev_box_widget_set_server_name(AudioDevBoxWidget *widget, const char *server_name) {
    g_return_if_fail(AUDIO_DEV_BOX_WIDGET(widget));
    gtk_label_set_text(GTK_LABEL(widget->priv->audio_dev_server_label),server_name);
}

static const char *audio_dev_box_widget_get_server_name(AudioDevBoxWidget *widget) {
    g_return_val_if_fail (AUDIO_DEV_BOX_WIDGET(widget), NULL);
    return gtk_label_get_text(GTK_LABEL(widget->priv->audio_dev_server_label));
}

static void audio_dev_box_widget_set_device_name(AudioDevBoxWidget *widget,const char *device_name) {
    g_return_if_fail(AUDIO_DEV_BOX_WIDGET(widget));
    gtk_label_set_text(GTK_LABEL(widget->priv->audio_dev_name_label),device_name);
}

static const char *audio_dev_box_widget_get_device_name(AudioDevBoxWidget *widget) {
    g_return_val_if_fail (AUDIO_DEV_BOX_WIDGET(widget), NULL);
    return gtk_label_get_text(GTK_LABEL(widget->priv->audio_dev_name_label));
}

static void audio_dev_box_widget_set_device_desc(AudioDevBoxWidget *widget, const char *description) {
    g_return_if_fail(AUDIO_DEV_BOX_WIDGET(widget));
    gtk_label_set_text(GTK_LABEL(widget->priv->audio_dev_desc_label),description);
}

static const gboolean audio_dev_box_widget_get_device_is_enabled(AudioDevBoxWidget *widget) {
    g_return_val_if_fail (AUDIO_DEV_BOX_WIDGET(widget), FALSE);
    return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget->priv->audio_dev_checkbutton));
}

static void audio_dev_box_widget_set_device_is_enabled(AudioDevBoxWidget *widget, gboolean is_enabled) {
    g_return_if_fail(AUDIO_DEV_BOX_WIDGET(widget));
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget->priv->audio_dev_checkbutton),is_enabled);
}

static const char *audio_dev_box_widget_get_device_desc(AudioDevBoxWidget *widget) {
    g_return_val_if_fail (AUDIO_DEV_BOX_WIDGET (widget), NULL);
    gtk_label_get_text(GTK_LABEL(widget->priv->audio_dev_desc_label));    
}

static void audio_dev_box_widget_set_is_available(AudioDevBoxWidget *widget, gboolean is_available) {
    g_return_if_fail(AUDIO_DEV_BOX_WIDGET(widget));
    if (is_available) {
        gtk_image_set_from_icon_name(GTK_IMAGE(widget->priv->audio_dev_available_img), DEVICE_AVAILABLE_STOCK,GTK_ICON_SIZE_MENU);
    } else {
        gtk_image_set_from_icon_name(GTK_IMAGE(widget->priv->audio_dev_available_img), DEVICE_NOT_AVAILABLE_STOCK,GTK_ICON_SIZE_MENU);        
    }
}

static const gboolean audio_dev_box_widget_get_is_available(AudioDevBoxWidget *widget) {
    g_return_val_if_fail (AUDIO_DEV_BOX_WIDGET (widget), NULL);
    const gchar **icon_name;
    gtk_image_get_icon_name(GTK_IMAGE(widget->priv->audio_dev_available_img),icon_name,NULL);
    return (strcmp(*icon_name,DEVICE_AVAILABLE_STOCK) == 0);
}


static void
audio_dev_box_widget_set_property (GObject         *object,
                         guint            prop_id,
                         const GValue    *value,
                         GParamSpec      *pspec)
{
  AudioDevBoxWidget *widget = AUDIO_DEV_BOX_WIDGET (object);

  switch (prop_id)
    {
      case PROP_SERVER_NAME:
          audio_dev_box_widget_set_server_name(widget, g_value_get_string(value));
          break;
    case PROP_DEVICE_NAME:
          audio_dev_box_widget_set_device_name(widget, g_value_get_string (value));
          break;
    case PROP_DEVICE_DESC:
          audio_dev_box_widget_set_device_desc(widget,g_value_get_string(value)); 
          break;
      case PROP_IS_AVAILABLE:
          audio_dev_box_widget_set_is_available(widget, g_value_get_boolean(value));
          break;
      case PROP_IS_ENABLED:
          audio_dev_box_widget_set_device_is_enabled(widget, g_value_get_boolean(value));
          break;          
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
audio_dev_box_widget_get_property (GObject         *object,
                        guint            prop_id,
                        GValue          *value,
                        GParamSpec      *pspec)
{
  AudioDevBoxWidget *widget = AUDIO_DEV_BOX_WIDGET (object);

  switch (prop_id)
    {
      case PROP_SERVER_NAME:
          g_value_set_string(value, audio_dev_box_widget_get_server_name(widget));
          break;
    case PROP_DEVICE_NAME:
      g_value_set_string (value, audio_dev_box_widget_get_device_name(widget));      
      break;
    case PROP_DEVICE_DESC:
      g_value_set_string (value, audio_dev_box_widget_get_device_desc(widget));    
      break;
    case PROP_IS_AVAILABLE:
      g_value_set_boolean(value, audio_dev_box_widget_get_is_available(widget));
      break;
      case PROP_IS_ENABLED:
          g_value_set_boolean(value,audio_dev_box_widget_get_device_is_enabled(widget));
          break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void audio_dev_box_widget_class_init(AudioDevBoxWidgetClass *class) {
    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(class);
    GObjectClass *gobject_class = G_OBJECT_CLASS(class);
    
    gobject_class->set_property=audio_dev_box_widget_set_property;
    gobject_class->get_property=audio_dev_box_widget_get_property;
    obj_properties[PROP_SERVER_NAME] =
            g_param_spec_string("server_name","Server Name","Server Name",NULL,G_PARAM_READWRITE);
    obj_properties[PROP_DEVICE_NAME] = 
            g_param_spec_string("device_name","Device Name","Device Name",NULL,G_PARAM_READWRITE);
    obj_properties[PROP_DEVICE_DESC] =
            g_param_spec_string("device_desc","Device Description","Device Description",NULL,G_PARAM_READWRITE);
    obj_properties[PROP_IS_AVAILABLE] =
            g_param_spec_boolean("device_is_available","Device Is Available","Device is Available",FALSE,G_PARAM_READWRITE);
    obj_properties[PROP_IS_ENABLED] =
            g_param_spec_boolean("device_is_enabled","Device Is Enabled","Device is Enabled",FALSE,G_PARAM_READWRITE);
    g_object_class_install_properties(gobject_class,N_PROPERTIES,obj_properties);
    
  gtk_widget_class_set_template_from_resource (widget_class,
                                               "/com/tuxin/tuxmusicstudio/audio_dev_box.glade");
 gtk_widget_class_bind_template_child_private(widget_class, AudioDevBoxWidget, audio_dev_server_label);
 gtk_widget_class_bind_template_child_private(widget_class, AudioDevBoxWidget, audio_dev_name_label);
 gtk_widget_class_bind_template_child_private(widget_class, AudioDevBoxWidget, audio_dev_desc_label);
 gtk_widget_class_bind_template_child_private(widget_class, AudioDevBoxWidget, audio_dev_available_img);
 gtk_widget_class_bind_template_child_private(widget_class, AudioDevBoxWidget, audio_dev_checkbutton);
 
 
 
}
